/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiran.midlet;

import java.util.Random;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author hiran
 */
public class PuzzleH extends MIDlet implements CommandListener{

    static final Command exitCommand = new Command("Exit", Command.STOP, 1);
    static final Command viewImage = new Command("View Image", Command.ITEM, 1);
    static final Command backFromImg = new Command("Back", Command.ITEM, 1);
    static final Command shuffle = new Command("Shuffle", Command.ITEM, 1);
    private Display theDisplay;
    PuzzleCanvas canvas;
    PuzzleImageCanvas imgCanvas;


    public PuzzleH()
    {
        theDisplay = Display.getDisplay(this);
    }
    public void startApp() {
        canvas = new PuzzleCanvas();
        canvas.addCommand(exitCommand);
        canvas.addCommand(viewImage);
	canvas.addCommand(shuffle);
        canvas.setCommandListener(this); 
        theDisplay.setCurrent(canvas);
	
        //doShuffle();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }

        if (c == viewImage)
        {
            commandViewImage();
        }
        if (c == backFromImg)
        {
            backToMain();
        }
        if (c == shuffle)
        {
            doShuffle();
        }


    }

    public void backToMain()
    {
	theDisplay.setCurrent(canvas);
    }

    public void commandViewImage()
    {
	imgCanvas = new PuzzleImageCanvas();
	imgCanvas.setCommandListener(this);
	imgCanvas.addCommand(backFromImg);
	theDisplay.setCurrent(imgCanvas);
    }

    public void doShuffle()
    {
        Random r = new Random();
        int i;
        for (int j=0;j<500;j++)
        {
            i = r.nextInt(4);
            canvas.randomized(i);
        }
    }
}